<?php
class PanelModel{
	
	public static function get_users_pages_list($__param ,$id_grup=null){
		global $config;
		$__lang = (isset($__param[1]) && !empty($__param[1])) ? $__param[1] : $config->__lang;
		//$__id_p_page = (isset($__param[0]) && !empty($__param[0])) ? $__param[0] : 0;
		$__q_get_users_pages = "SELECT a.id_p_page, b.*, a.id_grup, a.lp_page FROM ".PAGES." a 
			LEFT JOIN ".PLANG." b ON b.id_page = a.id_page 
			WHERE a.typ_page = 0 AND b.id_lang = '{$__lang}' 
			".((is_null($id_grup)) ?"":" AND id_grup = '{$id_grup}' ")."
			ORDER BY id_p_page, a.lp_page";
		$__result = tools::__adodb('getAll',$__q_get_users_pages);
		return $__result;
	}
	
	public static function get_page_data($id_page=null,$__lang=null){
		global $config;
		$__lang  = ( isset($__lang) && !is_null($__lang) ) ? $__lang : $config->__lang;
		if (PanelModel::sprawdz_czy_jest_wersja_jezykowa($id_page,$__lang )){
			$__q_get_page_data = "SELECT a.*, b.id_lang, b.page_name, b.page_url,b.description,b.keywords
				FROM ".PAGES." a LEFT JOIN ".PLANG." b ON b.id_page = a.id_page 
				WHERE a.id_page = '{$id_page}' AND b.id_lang = '{$__lang}'";
			$__result = tools::__adodb('getRow',$__q_get_page_data);
		}else{
			$__q_get_page_data = "SELECT a.*  FROM ".PAGES." a 
				WHERE a.id_page = '{$id_page}' " ;
			$__result = tools::__adodb('getRow',$__q_get_page_data);
			$__result['id_lang'] = $__lang;
		}
		$__s_result = tools::stripslashes_array($__result);
		return $__s_result;
	}
	
	private static function sprawdz_czy_jest_wersja_jezykowa( $id_page=null,$__lang=null ){
		global $config;
		$__lang  = ( isset($__lang) ) ? $__lang : $config->__lang;
		$__q_check_page_lang = "SELECT b.id_lang 
				FROM  ".PLANG." b 
				WHERE b.id_page = '{$id_page}' AND b.id_lang = '{$__lang}'";
		$__result = tools::__adodb('getOne',$__q_check_page_lang);
		return ( !empty($__result) && is_string($__result) ) ? true : false;
	}
	
	/**
	 * zwieksz lp strony
	 * 
	 * @param $id_kategoria
	 * @param $id_kategoria_parent
	 * @return unknown_type
	 */
	public static function up_lp_category($id_kategoria=null,$id_kategoria_parent=null){
		$q__ = "SELECT lp_page, id_grup FROM ".PAGES." WHERE id_page = {$id_kategoria}";
		$curent_row = tools::__adodb('getRow',$q__);
		$curent_lp__ = $curent_row['lp_page'];
		$new_lp__ = $curent_lp__-1;
		if ($curent_lp__ > 1){
			if(tools::__adodb('exec',"UPDATE ".PAGES." SET lp_page={$curent_lp__} WHERE lp_page = {$new_lp__} AND id_grup = '{$curent_row['id_grup']}' AND  id_p_page = {$id_kategoria_parent}"))
				tools::__adodb('exec',"UPDATE ".PAGES." SET lp_page={$new_lp__} WHERE id_page = {$id_kategoria}");
		}		
	}
	
	/**
	 * zmniejsz lp strony
	 * 
	 * @param $id_kategoria
	 * @param $id_kategoria_parent
	 * @return unknown_type
	 */
	public static function down_lp_category($id_kategoria=null,$id_kategoria_parent=null){
		$curent_row = tools::__adodb('getRow',"SELECT lp_page, id_grup FROM ".PAGES." WHERE id_page = {$id_kategoria}");
		$curent_lp__ = $curent_row['lp_page'];
		$max_lp__ = tools::__adodb('getOne',"SELECT MAX(lp_page) FROM ".PAGES." WHERE id_grup = '{$curent_row['id_grup']}' AND id_p_page = {$id_kategoria_parent}");
		$new_lp__ = $curent_lp__+1;
		if ($curent_lp__ < $max_lp__ ){
			if(tools::__adodb('exec',"UPDATE ".PAGES." SET lp_page={$curent_lp__} WHERE lp_page = {$new_lp__} AND id_grup = '{$curent_row['id_grup']}' AND id_p_page = {$id_kategoria_parent}"))
				tools::__adodb('exec',"UPDATE ".PAGES." SET lp_page={$new_lp__} WHERE id_page = {$id_kategoria}");
		}		
	}	
	
	
	/**
	 * pobranie listy sekcji i naniesienie sekcji edytowanej 
	 * strony
	 *
	 * @param array $__param
	 * @return array
	 */
	public static function get_page_sections($id_page = null,$__lang=null ){
		global $config;
		$__sections = unserialize(SECTI);
		
		$__lang = (isset($__lang) && !empty($__lang)) ? $__lang : $config->__lang;
		$__q_get_page_section = "SELECT a.id_section FROM ".THEME." a 
			WHERE a.id_page = '{$id_page}' AND a.id_lang = '{$__lang}'";
		$__result = tools::__adodb('getAll',$__q_get_page_section);
		
		foreach ($__result as $__section){
			$__i = 0;
			foreach ($__sections as $__row){
				if (array_key_exists($__section[0],$__row)) $__sections[$__i][$__section[0]] = 1;
				$__i++;
			}		
		}
		return $__sections;		
	}
	
	public static function get_section_data($__param){
		global $config;
		$__lang = (isset($__param[2]) && !empty($__param[2])) ? $__param[2] : $config->__lang;
		$__q_get_section_data = "SELECT a.page_name, a.page_url, b.*  
			FROM ".PLANG." a LEFT JOIN ".THEME." b ON b.id_page = a.id_page 
			WHERE b.id_page = '{$__param[0]}' AND b.id_section = '{$__param[1]}' 
			AND b.id_lang = '{$__lang}'";
		$__result = tools::__adodb('getRow',$__q_get_section_data);
		return $__result;
	}
	
	
	public static function save_upadate_page_data(){
		global $config;
		if (PanelModel::sprawdz_czy_jest_wersja_jezykowa($config->id_page,$config->id_lang)){
			$__q_update_page_data = "UPDATE ".PLANG." SET page_name = ".tools::__mres($config->page_name).", 
				page_url = ".tools::__mres(tools::__link($config->page_name,"_"))." , keywords = ".tools::__mres($config->keywords).",
				description = ".tools::__mres($config->description)."  
				WHERE id_page = ".tools::__mres($config->id_page)." 
				AND id_lang = ".tools::__mres($config->id_lang);
			$__result0 = tools::__adodb("exec",$__q_update_page_data);

		}else{
			$__q_update_page_data_new_lang = "INSERT INTO ".PLANG." (id_page, id_lang, page_name, page_url, description,keywords ) 
				VALUES (".tools::__mres($config->id_page).",".tools::__mres($config->id_lang).",
				".tools::__mres($config->page_name).",".tools::__mres(tools::__link($config->page_name,"_")).",
				".tools::__mres($config->description).",".tools::__mres($config->keywords)." )";
			$__result0 = tools::__adodb("exec",$__q_update_page_data_new_lang);
		}
		if (isset($config->defaultp) && $config->defaultp == 1 && $config->id_grup == 0 ){
			$__q_update_page_default = "UPDATE ".PAGES." SET defaultp = 0";
			tools::__adodb("exec",$__q_update_page_default);
		}
		
		$__q_update_page_data = "UPDATE ".PAGES." SET id_grup = ".tools::__mres($config->id_grup).", 
			plock = ".tools::__mres($config->plock).", redirect = ".tools::__mres($config->redirect).",
			to_url = ".tools::__mres($config->to_url).",theme = ".tools::__mres($config->theme).",
			defaultp = ".tools::__mres($config->defaultp)." 
			WHERE id_page = ".tools::__mres($config->id_page);
		$__result0 = tools::__adodb("exec",$__q_update_page_data);
		
		return $__result0;		
	}
	
	public static function delete_page_data($id_page=null){
		$__q_delete_page = "DELETE FROM ".PAGES." WHERE id_page = ".tools::__mres($id_page);
		$__result = tools::__adodb("exec",$__q_delete_page);
		$__q_delete_page = "DELETE FROM ".PLANG." WHERE id_page = ".tools::__mres($id_page);
		$__result = tools::__adodb("exec",$__q_delete_page);
		$__q_delete_page = "DELETE FROM ".THEME." WHERE id_page = ".tools::__mres($id_page);
		$__result = tools::__adodb("exec",$__q_delete_page);			
		return $__result;
	}
	
	
	public static function save_new_page_data(){
		global $dbl, $config;
		$lp = tools::__adodb('getOne',"SELECT MAX(lp_page) FROM ".PAGES." WHERE id_grup = '{$config->id_grup}' AND id_p_page = ".tools::__mres($config->id_p_page));
		$lp++;
		
		$__q_new_page_data = "
			INSERT INTO ".PAGES." (id_page,id_p_page,id_grup,lp_page,plock, redirect,to_url,theme, typ_page,defaultp)
			VALUES (NULL, ".tools::__mres($config->id_p_page).",".tools::__mres($config->id_grup).",'{$lp}',
			".tools::__mres($config->plock).",".tools::__mres($config->redirect).",
			".tools::__mres($config->to_url).",".tools::__mres($config->theme).",0,0)";
		$__result0 = tools::__adodb("exec",$__q_new_page_data);
		
		$__id_page = $dbl->lastInsertId();
		$_POST['id_page'] = $__id_page;
		$__q_new_page_data = "INSERT INTO ".PLANG."(id_page,id_lang,page_name,page_url,description,keywords) 
			VALUES ('{$__id_page}',".tools::__mres($config->id_lang).",".tools::__mres($config->page_name).",
			".tools::__mres(tools::__link($config->page_name,'_')).",".tools::__mres($config->description).",
			".tools::__mres($config->keywords)." );	";
			
		$__result1 = tools::__adodb("exec",$__q_new_page_data);	
		
		$__result = ($__result0 && $__result1 ) ? true : false;
		return $__result;	
	}
	
	public static function save_new_section_data(){
		global $config;
		$__q_insert_section_data = "INSERT INTO ".THEME." (id_page,id_section,id_lang,plock,
			section_body,section_body_hidden,section_body_sometime,
			last_modification,id_exten,exten_body) 
			VALUES (".tools::__mres($config->id_page).",".tools::__mres($config->id_section).",
			".tools::__mres($config->id_lang).",".tools::__mres($config->plock).",".tools::__mres($config->section_body).",
			".tools::__mres($config->section_body_hidden).",".tools::__mres($config->section_body_sometime).",'".date("Y-m-d H:i:s")."',
			".tools::__mres($config->exten_body).", '".(($config->__isset('exten_body') && $config->exten_body !="")?"{dispatcher->handleEvent v={$config->exten_body}}":"")."')";
		$__result = tools::__adodb("exec",$__q_insert_section_data);	
		return 	$__result;		
	}
	
	public static function save_upadate_section_data(){
		global $config;
		$__q_update_section_data = "UPDATE ".THEME." SET plock = ".tools::__mres($config->plock).", 
			section_body = ".tools::__mres($config->section_body).", 
			section_body_hidden = ".tools::__mres($config->section_body_hidden).",
			section_body_sometime = ".tools::__mres($config->section_body_sometime).",
			last_modification = '".date("Y-m-d H:i:s")."',
			id_exten = ".tools::__mres($config->exten_body).", exten_body = '".(($config->__isset('exten_body') && $config->exten_body !="")?"{dispatcher->handleEvent v={$config->exten_body}}":"")."'
			WHERE id_page = ".tools::__mres($config->id_page)." AND id_section = ".tools::__mres($config->id_section)."
			AND id_lang = ".tools::__mres($config->id_lang);
		$__result = tools::__adodb("exec",$__q_update_section_data);	
		return 	$__result;
	}
	
	public static function delete_section_data_from_page($id_page=null,$id_section=null, $id_lang = null){
		$__q_delete_section = "DELETE FROM ".THEME." WHERE id_page = ".tools::__mres($id_page)." 
			AND id_section = ".tools::__mres($id_section)." 
			AND id_lang = ".tools::__mres($id_lang);
		tools::__adodb("exec",$__q_delete_section);	
	}
}
?>